extends Node

var cpu_fish_s:Array = []
var gpu_fish_s:Array = []

var fish_count:int 

func _ready() -> void:
	pass

func _on_multi_mesh_pressed() -> void:
	clear()
	var id = 0
	var multi_mesh_node: MultiMeshInstance3D = $MultiMeshInstance3D
	var multi_mesh: MultiMesh = multi_mesh_node.multimesh
	multi_mesh.instance_count = fish_count
	multi_mesh.visible_instance_count = fish_count
	for i in fish_count:
		var t = Transform3D()
		t.origin.x =  randi() % 1000*0.02-6
		t.origin.y = randi() % 1000*0.02-6
		t.origin.z = randi() % 10000*-0.03
		gpu_fish_s.push_back(t)
		multi_mesh.set_instance_transform(id,t)
		id = id +1

func _on_cpu_pressed() -> void:
	clear()
	for i in fish_count:
		var f = preload("res://assets/CPU/Fish_cpu.scn").instantiate()
		cpu_fish_s.push_back(f)
		add_child(f)
		f.position.x = randi() % 1000*0.02-6
		f.position.y = randi() % 1000*0.02-6
		f.position.z = randi() % 10000*-0.03


func _process(delta: float) -> void:
	fish_count = $CanvasLayer/LineEdit.text.to_int()
	$CanvasLayer/Label.text = "FPS : " + String.num(Engine.get_frames_per_second())
	for i in cpu_fish_s:
		i.position.z += delta*1
	var multi_mesh_node: MultiMeshInstance3D = $MultiMeshInstance3D
	var multi_mesh: MultiMesh = multi_mesh_node.multimesh
	for i in gpu_fish_s.size():
		gpu_fish_s[i].origin.z += delta*1
		multi_mesh.set_instance_transform(i,gpu_fish_s[i])

func clear():
	for i in cpu_fish_s:
		i.queue_free()
	cpu_fish_s.clear()
	gpu_fish_s.clear()
	var multi_mesh_node: MultiMeshInstance3D = $MultiMeshInstance3D
	var multi_mesh: MultiMesh = multi_mesh_node.multimesh
	multi_mesh.instance_count = 0
	multi_mesh.visible_instance_count = 0
